package com.xiaoye.controller;

import com.xiaoye.constant.Leader;
import com.xiaoye.constant.Role;
import com.xiaoye.dto.ResponseResult;
import com.xiaoye.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author 宵夜
 * @version 1.0.0
 * @Description: TODO
 */
@RestController
@Slf4j
public class LoginController extends BaseController {

    ResponseResult result = new ResponseResult();

    @PostMapping("/login")
    @ResponseBody
    public ResponseResult login(String no, String password, String role) {
        log.info("请求登录的用户信息 : {}", "no = " + no, "password = " + password, "role = ", role);
        User user = null;
        if (Role.ADMIN.equals(role)) {
            //admin登录
            Admin build = Admin.builder()
                    .no(no).password(password)
                    .build();
            List<Admin> admins = adminService.listAdmins(build);
            if (admins.size() > 0) {
                //找到登陆的用户并放入session
                Admin admin = admins.get(0);
                user = User.builder()
                        .id(admin.getId())
                        .role(role)
                        .no(no)
                        .name("管理员[" + admin.getId() + "]")
                        .img(admin.getImg())
                        .build();
                result.setCode(200);
                result.setMessage("管理员登录成功");
            } else {
                //判断账号是否存在
                build.setPassword(null);
                List<Admin> existAdmin = adminService.listAdmins(build);
                if (existAdmin.size() > 0) {
                    //密码错误
                    result.setCode(100);
                    result.setMessage("密码错误");
                } else {
                    //账户不存在
                    result.setCode(101);
                    result.setMessage("账户不存在");
                }
            }
        }
        if (Role.STUDENT.equals(role)) {
            //学生登录
            Student build = Student.builder()
                    .no(no)
                    .password(password)
                    .build();
            List<Student> students = studentService.listStudents(build);
            if (students.size() > 0) {
                //找到登录的用户并放入session
                Student student = students.get(0);
                user = User.builder()
                        .id(student.getId())
                        .name(student.getName())
                        .no(no)
                        .img(student.getImg())
                        .role(Role.STUDENT)
                        .build();
                result.setCode(200);
                result.setMessage("学生登陆成功");
            } else {
                //判断账户是否存在
                build.setPassword(null);
                List<Student> existStudents = studentService.listStudents(build);
                if (existStudents.size() > 0) {
                    //密码错误
                    result.setCode(100);
                    result.setMessage("密码错误");
                } else {
                    //账户不存在
                    result.setCode(101);
                    result.setMessage("账户不存在");
                }
            }

        }
        if (Role.TEACHER.equals(role)) {
            //老师登录
            Teacher build = Teacher.builder()
                    .no(no).password(password).build();
            List<Teacher> teachers = teacherService.listTeachers(build);
            if (teachers.size() > 0) {
                //找到登陆的的用户放入session中
                Teacher teacher = teachers.get(0);
                user = User.builder()
                        .id(teacher.getId())
                        .no(no)
                        .role(Role.TEACHER)
                        .img(teacher.getImg())
                        .name(teacher.getName())
                        .build();
                result.setCode(200);
                result.setMessage("老师登陆成功");
                if (teacher.getIsCollegeLeader().intValue() == Leader.YES.intValue()) {
                    // 学院领导
                    user.setIsLeader(Leader.YES);
                }
            } else {
                //判断账户是否存在
                build.setPassword(null);
                List<Teacher> existTeachers = teacherService.listTeachers(build);
                if (existTeachers.size() > 0) {
                    //密码错误
                    result.setCode(100);
                    result.setMessage("密码错误");
                } else {
                    //账户不存在
                    result.setCode(101);
                    result.setMessage("账户不存在");
                }
            }
        }
        if (Role.DEPARTMENT.equals(role)) {
            Department build = Department.builder()
                    .no(no)
                    .password(password)
                    .build();
            List<Department> departments = departmentService.listDepartments(build);
            if (departments.size() > 0) {
                //把登录的用户放入session
                Department department = departments.get(0);
                user = User.builder()
                        .no(no)
                        .id(department.getId())
                        .name(department.getName())
                        .role(Role.DEPARTMENT)
                        .img(department.getImg())
                        .build();
                result.setCode(200);
                result.setMessage("部门登录成功");
            } else {
                //判断账户是否存在
                build.setPassword(null);
                List<Department> existDepartment = departmentService.listDepartments(build);
                if (existDepartment.size() > 0) {
                    //密码错误
                    result.setCode(100);
                    result.setMessage("密码错误");
                } else {
                    //账户不存在
                    result.setCode(101);
                    result.setMessage("账户不存在");
                }
            }
        }
        session.setAttribute("loginUser", user);
        return result;
    }


    /**
     * 退出登录
     */
    @GetMapping("logout")
    public ModelAndView logout() {
        session.invalidate();
        return new ModelAndView("redirect:/login.html");

    }


    /**
     * 修改密码
     */
    @PostMapping("reset")
    @ResponseBody
    public ResponseResult reset(String oldPassword, String newPassword) {
        String role = loginUser.getRole();
        if (Role.ADMIN.equals(role)) {
            // 管理员登录
            if (loginAdmin.getPassword().equals(oldPassword)) {
                //旧密码正确
                loginAdmin.setPassword(newPassword);
                if (adminService.update(loginAdmin)) {
                    result.setCode(200);
                    result.setMessage("密码修改成功");
                } else {
                    result.setCode(101);
                    result.setMessage("旧密码与新密码重复");
                }
            } else {
                result.setCode(501);
                result.setMessage("原密码错误");
            }

        }
        if (Role.TEACHER.equals(loginUser.getRole())) {
            //老师登录
            if (loginTeacher.getPassword().equals(oldPassword)) {
                //旧密码正确
                loginTeacher.setPassword(newPassword);
                if (teacherService.update(loginTeacher)) {
                    result.setCode(200);
                    result.setMessage("密码修改成功");
                } else {
                    result.setCode(101);
                    result.setMessage("旧密码与新密码重复");
                }
            } else {
                result.setCode(501);
                result.setMessage("原密码错误");
            }
        }
        if (Role.STUDENT.equals(loginUser.getRole())) {
            //学生登录
            if (loginStudent.getPassword().equals(oldPassword)) {
                //旧密码正确
                loginStudent.setPassword(newPassword);
                if (studentService.update(loginStudent)) {
                    result.setCode(200);
                    result.setMessage("密码修改成功");
                } else {
                    result.setCode(101);
                    result.setMessage("旧密码与新密码重复");
                }
            } else {
                result.setCode(501);
                result.setMessage("原密码错误");
            }
        }

        if (Role.DEPARTMENT.equals(loginUser.getRole())) {
            //部门登录
            if (loginDepartment.getPassword().equals(oldPassword)) {
                //密码正确
                loginDepartment.setPassword(newPassword);
                if (departmentService.update(loginDepartment)) {
                    result.setCode(200);
                    result.setMessage("密码修改成功");
                } else {
                    result.setCode(101);
                    result.setMessage("旧密码与新密码重复");
                }
            } else {
                result.setCode(501);
                result.setMessage("原密码错误");
            }
        }
        if(result.getCode() == 200){
            session.invalidate();
        }
        return result;
    }
    }




